Welcome to Express Web Application Framework. After watching this video, you will be able
to describe the Express framework, explain the primary uses of Express, and download Express. Express is a web application framework based
on the Node.js runtime environment, however, Express abstracts low-level details. Express helps you organize your application
better and develop your application faster. It provides robust mechanisms for integrating
middleware packages and handling different hypertext transfer protocol (HTTP) request
methods. The Express web application framework is widely
used in the field today and forms the basis of other frameworks as well. Express is primarily used for two purposes,
one as an API, and the second is to set up templates with server-side rendering, also
known as SSR. Using Express to build APIs means setting
up an HTTP interface to interact with the data layer. In the case of APIs, data is sent back to
the client in JSON format using a response object, or res for short. The res.json method is used to notify the
client of the content type of the data being sent, such as if it's an image or text. It can also be used to Stringify data,
in other words, converting JavaScript to JSON format. In Server-side rendering (SSR), Express is
used to set up templates. Express is responsible for using the data
sent from the client supplied by an HTTP request in conjunction with the template to dynamically
write HTML, CSS, and/or JavaScript. The HTML, CSS, and/or JavaScript is sent back
to the client as text so that the browser can render the page. This is accomplished using the Express res.render
method. Express implements an app class that you map
to a web resource path. In contrast, when using the Node.js core application
programming interface (API), the http.createServer function relies on your custom callback function
to parse through the web resource path. Here is an overview of how to work with Express. There are five steps. Step one, declare Express as a dependency
in the package manifest of a Node.js project. Two, run the npm command to download any missing
modules. Three, import the Express module and create
an Express application. Four, create a new route handler. And five,
start an HTTP server on a given port number. Next, you will see how to complete steps one
and two. First, to declare Express as a dependency
in the package.json file, create a package.json file in your project folder. The package.json file stores information about
the contents of a Node.js module including five items: name, version, description, main,
and dependencies. Name is a name for the Node.js module. Version is a string that defines the major
and minor version number of the module. Description is a sentence that describes the
purpose of the module. Main identifies the Node.js script as the
entry point into the module. And dependencies is a list of which Node.js
modules that the current module requires. To declare Express as a dependency, list the
Express module and a version number in the dependencies property. Here, In this package.json dependency file,
the name of the module is temperature. Its version is 1.0.0. Its description is "retrieve current weather
conditions in the United States." The main script file, which is the entry point
in the module, is set to app.js. It has a dependency on the Express framework,
which is version 4.x. In step 2, you run the npm command to download
any missing modules. When you run the npm install command inside
the Node.js module directory, it resolves any missing dependent Node.js modules. The command downloads and saves any missing
Node.js modules into its own node_module directory. This scheme allows you to use different versions
of the same Node.js module in different packages. For example, a custom Node.js module installs
a version 4.0 release of the Express framework. Another Node.js module installs version 5.0
in its own Node module repository. To install a Node.js module into the shared
node_module directory, run npm install with your module name from the directory with your
Node.js installation. To download Express, run npm install in your
project folder When you run the command npm install with
no parameters, the npm application scans your package.json file. It checks your node_module directory to see
if any modules are missing. In this case, the Express web application
framework is not in your current installation. The npm application downloads the Express
framework and all of its dependent modules. In this video, you learned that: Express is a third-party module that provides
a framework for building web applications. Express serves two purposes: to set up APIs and to implement SSR And to install Express, there are five steps: Declare Express as a dependency in the package
manifest of a Node.js project. Run the npm command to download any missing
modules. Import the Express module and create an Express
application. Create a new route handler. And finally, start an HTTP server on a given port
number.